home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 24 < prev    next >
Encoding:
Text File  |  1996-08-06  |  5.1 KB  |  130 lines

  1. Newsgroups: comp.std.c
  2. Path: netcom.com!ahicks
  3. From: ahicks@netcom.com (Aaron Hicks at Netcom)
  4. Subject: SUMMARY: static and extern, and a variable be both ?
  5. Message-ID: <ahicksDKo2Ap.GKF@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. Date: Thu, 4 Jan 1996 17:08:48 GMT
  8. Sender: ahicks@netcom3.netcom.com
  9.  
  10. Folks,
  11.  
  12. I have summarized the answers I got about my problem with the static and 
  13. extern keywords below.  But first I would like to thank the following folks
  14. for being so kind as to enlighten me as to how this works.
  15.  
  16. Crawford@cris.com
  17. seebs@intran.xerox.com
  18. fgae07@ods05.and.ifg.gmeds.com
  19. mjs@hubcap.clemson.edu
  20. weather@netside.com
  21.  
  22. SUMMARY:
  23.  
  24.   If a variable is declared static inside a function this means to keep it 
  25. in memory along with the globals and to allocate this space at run time.  If 
  26. a global is declared as static ( as I was doing ) it is hidden from all other 
  27. modules ( .c files ).  So a static in a function can be an extern for another 
  28. module but a global static can not be seen from other modules.
  29.  
  30.   I have to admit that in hind sight a static global is kind of redundant in
  31. the context of what we wanted to do with this variable.
  32.  
  33.   After this message I have included the responces I got from the group.  I 
  34. have removed the header and most of the signatures of each message and any
  35. part that included my original post.
  36.  
  37. Once again Thank You all for you help
  38.  
  39. Aaron Hicks
  40. -------------------------------------------------------------------------
  41. Crawford@cris.com wrote
  42.  
  43.     When an object (variable, array) is declared with global scope
  44. (not declared within a function), it is permanently allocated and
  45. available for external linking. When you declare a global variable
  46. static, you are limiting access to it to the code in that
  47. translation unit (source file).
  48.  
  49.     I think you're confusing the meaning of static within
  50. functions, where it means "allocate at compile time", and the meaning
  51. of static for global variables: "make available only to this source
  52. file". All global variables are allocated at compile time, so the
  53. modifier of static is used to change the object's scope.
  54.  
  55.     It's confusing, but it makes sense after you've used it a
  56. while.
  57.  
  58. -------------------------------------------------------------------------
  59. seebs@intran.xerox.com wrote
  60.  
  61. You are confused.
  62. static *inside a function* causes only one copy of a variable to be
  63. created.
  64.  
  65. static *outside* a function is specifically to prevent other modules
  66. (other .c files) from being allowed to see the variable.
  67.  
  68. -s
  69.  
  70. ------------------------------------------------------------------------
  71. fgae07@ods05.and.ifg.gmeds.com wrote
  72.  
  73. When you declare something static to a module, that means only the
  74. functions in that module can reference the variable.  Only functions in
  75. the raster.c module can refer to bit_fail_array_table and have it
  76. defined.  When you declare bit_fail_array_table to be extern in
  77. raster_analysis.c, it expects another module to actually define it,
  78. which causes storage to ba allocated.  Since the variable in raster.c is
  79. only visible within raster.c, raster_analysis.c cannot see it, hence the
  80. message about the variable being undefined.  When you took off the
  81. static declaration in raster.c, this made the variable a system-wide
  82. global, visible to anybody who has an external declaration for it.
  83.  
  84. Hope this helps.
  85.  
  86. -- 
  87. Joe Gilliatt
  88. fgae07@ods05.and.ifg.gmeds.com
  89.  
  90. ------------------------------------------------------------------------
  91. mjs@hubcap.clemson.edu wrote
  92.  
  93. If bit_fail_array_table is at file scope (outside of any function)
  94. then it has static extent (i.e. it lives for the entire program), no
  95. matter what.  In this context, the static modifier means that the
  96. variable name is invisible outside of the file.  This is a problem if
  97. the variable is declared extern in another file.  The solution is
  98. simply to not declare it static in the defining file.  But it's good
  99. practice to explicitly initialize it, even if it is to be initialized
  100. to zero, just to indicate that it is the defining instance.
  101.  
  102. If a variable has block scope (inside a function), it can never be
  103. visible outside the block it appears in, and then the static modifier
  104. indicates that it has static (as opposed to automatic) extent.
  105.  
  106.         Matthew Saltzman
  107.         Clemson University Math Sciences
  108.         mjs@clemson.edu
  109.  
  110. -----------------------------------------------------------------------------
  111. weather@netside.com wrote 
  112.  
  113.    The purpose of using the 'static' keyword on a global variable is *precisely*
  114. to prevent it from being accessed outside of the translation unit, i.e. other
  115. source files.  Using static within a function has another purpose in that
  116. the variable so declared will retain its value.  You must remember that
  117. local variables within functions are generally given temporary storage on the 
  118. stack, which is lost when the function returns.  Declaring a local variable
  119. as static will cause it to be stored in memory alongside global variables
  120. even though it still retains local scope.  Using 'static' on global variables
  121. only makes its scope or 'viewability' limited.  In short, get rid of the
  122. 'static'.  Hope this helps.
  123.  
  124. R.Weathersby
  125.  
  126. P.S. I hope I interpreted your message correctly.
  127.  
  128.  
  129.  
  130.